<?php
declare (strict_types = 1);
namespace app\admin\model\data;

use app\user\logic\user\Account;

/**
 * 用户支付记录
 */
class UserPaymentData extends DataModel
{

    /**
     * 表名
     * @var string
     */
    protected $name = 'user_payment_data';

    /**
     * 设置自动写入时间
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    /**
     * 时间显示格式
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';
    /**
     * 购买者
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-04
     * @return   [type]                         [description]
     */
    public function user()
    {
        return $this->belongsTo(Account::class, 'user_id');
    }

    /**
     * 某段时间付费的新用户数量
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-16
     * @param    array 时间区间
     * @return   [type]                                   [description]
     */
    public static function getPaymentUserCountByTimespan(array $timespan)
    {
        return UserPaymentData::scope('mhm', 'user.mhm_id')->withJoin(['user' => ['mhm_id']])->whereBetween('__TABLE__.create_time', $timespan)->count();
    }
}
